Ext.define('Pearlsen.PAjaxProxy', {
	extend: 'Ext.data.proxy.Ajax',
    alias: 'proxy.pajax',
    buildRequest: function(operation) {
     	var params = Ext.applyIf(operation.params || {}, this.extraParams || {}),
            request;
        params = Ext.applyIf(params, this.getParams(operation));

        if (operation.id && !params.id) {
            params.id = operation.id;
        }
        params._json_message =this.buildQuery(params);
        request = Ext.create('Ext.data.Request', {
            params   : params,
            action   : operation.action,
            records  : operation.records,
            operation: operation,
            url      : operation.url
        });

        request.url = this.buildUrl(request);
        operation.request = request;

        return request;
     
    },
    buildQuery:function(params){
    	var _query = new Query(params.handlerName,params.datasetName);
    	_query.setStart(params.start);
    	_query.setPageSize(params.limit);    	
    	_query.setHandlerMethod(params.handlerMethod);
    	_query.setConditions(params.conditions);
    	return _query.toJson();
    },
    _getQueryConditions:function(conditions){
    	var result=[];
    	if( conditions && conditions.length > 0 ) {
    		for( var i = 0; i < conditions.length ; i++ ){
    			var c = conditions[i];
    			for( var prop in c ){
    				result.push( new Condition(prop,c[prop]) );
    			}
    		}
    	}
    	return result;
    }
  
});